<!--
* description: 服务活动查询-备件
* author: wangjuan
* createdDate: 2021-3-11
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-table-defult-height="tableHeight"
      :callback-obj="callbackObj"
      :is-show-pagination="false"
    />

    <!-- 工时选择 -->
    <timeToChoose
      :key="editPopupsKey1"
      :dynamic-edit-row-data="timeEditRowData"
      :popups-visible-time="popupsVisibleTime"
      @close="close1"
      @returnTimeData="returnTimeData"
    />
  </div>
</template>
<script>

import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'

export default {
  name: 'WorkingHours',

  components: {
    OneTableTemplate,
    // 弹窗
    timeToChoose: () => import('./component/timeToChoose.vue')

  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  props: {

  },

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {

      tableHeight: 300,
      editPopupsKey1: 'editPopupsKey1',
      timeEditRowData: {},
      popupsVisibleTime: false,
      timeDatas: [],
      selectTime: [],

      tableButtons: [
        {
          compKey: 'btnKey1',
          size: 'small',
          name: 'add',
          clickEvent: () => this.timeAddd(),
          text: '新增',
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'cancel',
          clickEvent: () => this.deleteTime(),
          text: '删除',
          position: 'right'
        }
      ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'wiId',
              label: '工时ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'wiCode',
              label: '工时编码'
            },
            {
              prop: 'repairContent',
              label: '维修内容'
            },
            {
              prop: 'saleWiQty',
              label: '工时数'
            },
            {
              prop: 'wiPrice',
              label: '工时单价'
            },
            {
              prop: 'wiAmount',
              label: '费用合计'
            }
          ],

      // 表单查询数据
      formField: this.$utils.getFormField(this, {

      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode]),

      callbackObj: {
        onCheckSelectChange: (selection) => {
          this.selectTime = selection
        }
      }
    }
  },

  watch: {
    timeDatas(val) {
      for (let i = 0; i < this.timeDatas.length; i++) {
        this.timeDatas[i].wiAmount = (Number((this.timeDatas[i].wiPrice * 100) / 100) * this.timeDatas[i].saleWiQty).toFixed(2)
      }
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 工时新增
    timeAddd() {
      this.popupsVisibleTime = true
    },

    // 弹窗返回数据判断及转换字段
    returnTimeData(returnData) {
      const timeTableData = this.$refs.multipleTable.list
      for (let i = 0; i < timeTableData.length; i++) {
        if (timeTableData[i].wiCode === returnData.wiCode) {
          this.$message({
            message: '此条数据已添加！',
            duration: 1000
          })
          return
        }
      }
      this.$refs.multipleTable.list.push(returnData)
      this.timeDatas = this.$refs.multipleTable.list
    },

    // 删除
    deleteTime() {
      if (!this.selectTime.length) return this.$message('请先勾选删除数据！')
      for (const i of this.selectTime) {
        this.$refs['multipleTable'].list = this.$refs['multipleTable'].list.filter(
          (item) => item !== i
        )
      }
      this.timeDatas = this.$refs['multipleTable'].list
    },

    // 关闭
    close1() {
      this.popupsVisibleTime = false
    }

  }
}
</script>

